package club.vann.mock.factory;

import club.vann.mock.biyi.acceptor.AcceptorService;
import club.vann.mock.model.enums.StrategyAction;
import java.util.EnumMap;
import java.util.Map;
import java.util.Optional;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

/** Acceptor业务方法工厂，负责加载所有 Acceptor 业务方法。 */
@Component
public class AcceptorServiceFactory implements ApplicationContextInitializer {
  private static final Map<StrategyAction, AcceptorService> MAPPING =
      new EnumMap<>(StrategyAction.class);

  public Optional<AcceptorService> get(StrategyAction action) {
    return Optional.ofNullable(MAPPING.get(action));
  }

  @Override
  public void initialize(ConfigurableApplicationContext applicationContext) {
    Map<String, AcceptorService> beansOfType =
        applicationContext.getBeansOfType(AcceptorService.class);
    beansOfType.forEach((k, v) -> MAPPING.put(v.action(), v));
  }
}
